package com.aaa.mrcai.service.impl;

import com.aaa.mrcai.mapper.DepartMapper;
import com.aaa.mrcai.pojo.entity.DepartTree;
import com.aaa.mrcai.pojo.entity.Dept;
import com.aaa.mrcai.pojo.entity.Permission;
import com.aaa.mrcai.pojo.entity.State;
import com.aaa.mrcai.pojo.vo.DepartVO;
import com.aaa.mrcai.query.DeptQuery;
import com.aaa.mrcai.result.AjaxResult;
import com.aaa.mrcai.service.DepartService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TODO
 *
 * @author 惠普
 * @date 2021/12/2  20:51
 */
@Service
public class DepartServiceImpl implements DepartService {

    @Autowired
    private DepartMapper departMapper;

    @Autowired
    private HttpSession session;

    @Override
    public AjaxResult list(DeptQuery query) {
        //当前页信息
        /*List<Dept> depts = departMapper.listDepart(query);*/
        List<Dept> depts = departMapper.listDepart(query);

        System.out.println(depts);
        //符合条件总数量
        Long departTotal = departMapper.getDepartTotal(query);

       //打包发送
        Map<String, Object> map = new HashMap<>();
        map.put("pageData",depts);
        map.put("totalCount",departTotal);
        System.out.println(map);
        return AjaxResult.success(map);

    }

    @Override
    public AjaxResult listState() {
        List<State> state = departMapper.listState();
        return AjaxResult.success(state);
    }

    @Override
    public AjaxResult addDepart(DepartVO depart) {
        //0.判断部门是否已经存在
        List<Dept> deptExist = departMapper.isDeptExist(depart);
        if(deptExist.size()>0){
            return AjaxResult.defeated("对不起，该部门已存在");
        }else {
            //1.不存在则添加部门
            int i = departMapper.addDepart(depart);
            //2.判断操作结果并返回
            if(i>0){
                return AjaxResult.success("添加成功！");
            }else {
                return AjaxResult.defeated("添加失败");
            }
        }
    }

    @Override
    public AjaxResult getDepart(int id) {
        Dept depart = departMapper.getDepart(id);
        session.setAttribute("currentDepart",depart);
        return AjaxResult.success(depart);
    }

    @Override
    public AjaxResult delDepart(int id) {
        int i = departMapper.delDepart(id);
        if(i>0){
            return AjaxResult.success();
        }else {
            return AjaxResult.defeated();
        }
    }

    @Override
    public AjaxResult updateDepart(DepartVO dept) {
        //判断是否修改的是该部门
        Dept currentDepart = (Dept) session.getAttribute("currentDepart");

        if(!dept.getId().equals(currentDepart.getId())){
            return AjaxResult.defeated("请确认是本人操作");
        }
        //判断该部门是否存在
        Long updateDeptExist = departMapper.isUpdateDeptExist(dept);
        if(updateDeptExist>0){
            return AjaxResult.defeated("该部门已经存在啦");
        }else {
            //修改部门信息
            int i = departMapper.updateDepart(dept);
            if(i>0){
                return AjaxResult.success("修改成功");
            }else {
                return AjaxResult.defeated("修改失败");
            }
        }

    }

    @Override
    public AjaxResult listTree() {
        List<DepartTree> departTrees = departMapper.listTree();

        //整理树形数据
        ArrayList<DepartTree> haha = new ArrayList<>();
        for (DepartTree departTree : departTrees) {
            String type = departTree.getType();
            if("D".equals(type)){
                haha.add(departTree);
            }
        }
        for (DepartTree tree : departTrees) {
            String type = tree.getType();
            if(!"D".equals(type)){
                for (DepartTree departTree : haha) {
                    Integer id = departTree.getId();
                    if(id.equals(tree.getDid())){
                        departTree.getChildren().add(tree);
                    }
                }
            }

        }
        System.out.println(haha);

        return AjaxResult.success(haha);
    }

    @Override
    public AjaxResult listSubDepart(DeptQuery query) {
        //当前页信息

        List<Dept> depts = departMapper.listSubDepart(query);
        return AjaxResult.success(depts);

    }

    @Override
    public AjaxResult listSupDepart() {
        List<Dept> depts = departMapper.listSupDepart();
        return AjaxResult.success(depts);
    }

    @Override
    public AjaxResult listType() {
        List<Dept> depts = departMapper.listType();
        return AjaxResult.success(depts);
    }

/*
    @Override
    public AjaxResult downloadInfo(DeptQuery query) {
        return null;
    }

*/

}
